// 获取token
export function getToken() {

    // if (uni.getStorageSync('token')) {
    //   return window.token
    // } else {
    //   //暂时先用这个token过期联系我
     // return 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIwMDA6YWRtaW4iLCJleHAiOjE3NjQ1MjI0MzZ9.V3qIh9mdpDFdd5Cf4MYiI-Sdjy4FPRm_hQiT-QqUeuzMYsLapyPCiKo7GWIyaMdhUVT0RvSo3n7h4FeYIQbnjw'
    // }
      return localStorage.getItem('token')
}

// 消息提示
export function message(title) {
    uni.showToast({
        icon: 'none',
        title: title,
    })
}
//获取屏幕大小
export function getScreen(title) {
    let width, height
    uni.getSystemInfo({
        success: function (res) {
            width = res.windowWidth
            height = res.windowHeight
        }
    })
    return {width, height}
}

//计算经纬度之间距离
export function getDistance(lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;

    var rad1 = lat1 * Math.PI / 180.0;
    var rad2 = lat2 * Math.PI / 180.0;
    var a = rad1 - rad2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var r = 6378137;
    var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));

    return distance;
}

//保存图片
export function saveImage(e) {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: 'https://www.zjjqf.cn/api/common/uploadImage',
            filePath: e,
            name: 'file',
            header: {
                Authorization: getToken()
            },
            formData: {
                'user': 'test'
            },
            success: (res) => {
                const mg = JSON.parse(res.data)
                if (mg.status === 200) {
                    const {path} = mg.data
                    resolve(path)
                    // uni.showToast({title:mg.msg,icon:'none'})
                } else {
                    uni.showToast({title: mg.msg, icon: 'none'})
                }

            }
        });
    })
}

//防抖
export const debounce = (fn, time = 300) => {
    let timeOut = null
    return function () {
        timeOut && clearTimeout(timeOut)
        timeOut = setTimeout(() => {
            fn.call(this, ...arguments)
        }, time)
    }
}

//商品图片处理
export function setImage(str) {
    let imagePath = 'http://129.211.211.126/download/defaultGoods.png'
    if (str) {
        try {
            const list = JSON.parse(str)
            if (list)
                imagePath = list[0].path
        } catch (e) {
            return imagePath
        }
    }
    return imagePath
}

//获取当天日期
export function curDate() {
    const date = new Date()
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) month = '0' + month
    if (day < 10) day = '0' + day
    return `${date.getFullYear()}-${month}-${day}`
}

//获取本月日期
export function curMonthDate() {
    const date = new Date()
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) month = '0' + month
    if (day < 10) day = '0' + day
    return [`${date.getFullYear()}-${month}-01`, `${date.getFullYear()}-${month}-${day}`];
}
//获取本周日期
export function curWeekDate() {
    var today = new Date();
    var currentDayOfWeek = today.getDay();
    var offset = currentDayOfWeek - 1;
    if (offset < 0) {
        offset = 6;
    }
    var monday = new Date(today.getTime() - (offset * 24 * 60 * 60 * 1000));
    let month = monday.getMonth() + 1
    let day = monday.getDate()
    if (month < 10) month = '0' + month
    if (day < 10) day = '0' + day
    return [`${monday.getFullYear()}-${month}-${day}`,curDate()]

}

export function curTime() {
    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
    var day = ('0' + currentDate.getDate()).slice(-2);
    var hours = ('0' + currentDate.getHours()).slice(-2);
    var minutes = ('0' + currentDate.getMinutes()).slice(-2);
    const seconds = ('0' + currentDate.getSeconds()).slice(-2);
    var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    return formattedDate
}
//计算分辨率基数
export  function getBaseRatio(){
    const {width} = getScreen()
    return width / 750
}


//获取昨天日期
export function getYesterdayDate() {
    //获取昨天日期yyyy-mm-dd
    const date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000)
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) month = '0' + month
    if (day < 10) day = '0' + day
    return `${date.getFullYear()}-${month}-${day}`;
};


let _user = {};
export function setGlobalUser(user){
    _user = user;
}
export function getGlobalUser(user){
    if(_user.userId){
        return _user;
    }
    throw new Error('为获取到用户信息，请先调用setGlobalUser方法');
}
//返回上一页
export function backPage(){
    history.back()
}